package com.yanqu.road.dao.impl.cross.marriage;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.enums.eMarriageStatusType;
import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMarriageDaoImpl extends TempDataObjectDao<MarriageInfo> {

    public Map<String, MarriageInfo> getMarriageInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, MarriageInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_marriage where status<>2";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                MarriageInfo data = parse(rs);
                dataMap.put(data.getChildUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(MarriageInfo data) {
        PreparedStatement call = null;
        String sql = "replace into t_c_marriage(userId,userNickName,status,childUid,childTalent,childOccupation,childName,childSex,childHeadIcon,"
                + "childAdultTime,publishTime,appointUserId,reward,motherBeautyId,childOccupationId,childEarnSpeed,childOccupationAddition,isTwins,"
                + "limitEarnSpeed,serverId,partnerServerId,marriageConsume) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getUserNickName());
            call.setInt(i++, data.getStatus().getValue());
            call.setString(i++, data.getChildUid());
            call.setInt(i++, data.getChildTalent());
            call.setInt(i++, data.getChildOccupation());
            call.setString(i++, data.getChildName());
            call.setInt(i++, data.getChildSex());
            call.setInt(i++, data.getChildHeadIcon());
            call.setLong(i++, data.getChildAdultTime());
            call.setLong(i++, data.getPublishTime());
            call.setLong(i++, data.getAppointUserId());
            String reward = PropertyHelper.parsePropertyToString(data.getReward());
            call.setString(i++, reward);
            call.setInt(i++, data.getMotherBeautyId());
            call.setInt(i++, data.getChildOccupationId());
            call.setString(i++, data.getChildEarnSpeed().toString());
            call.setInt(i++, data.getChildOccupationAddition());
            call.setBoolean(i++, data.isTwins());
            call.setString(i++, data.getLimitEarnSpeed().toString());
            call.setLong(i++, data.getServerId());
            call.setLong(i++, data.getPartnerServerId());
            call.setString(i++, data.getMarriageConsume());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(MarriageInfo data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_c_marriage SET userNickName=?,status=?,childTalent=?,childOccupation=?,childName=?,childSex=?,childHeadIcon=?,"
                + "childAdultTime=?,publishTime=?,appointUserId=?,reward=?,motherBeautyId=?,childOccupationId=?,childEarnSpeed=?,"
                + "childOccupationAddition=?,isTwins=?,limitEarnSpeed=?,serverId=?,partnerServerId=?,marriageConsume=? where userId=? and childUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getUserNickName());
            call.setInt(i++, data.getStatus().getValue());
            call.setInt(i++, data.getChildTalent());
            call.setInt(i++, data.getChildOccupation());
            call.setString(i++, data.getChildName());
            call.setInt(i++, data.getChildSex());
            call.setInt(i++, data.getChildHeadIcon());
            call.setLong(i++, data.getChildAdultTime());
            call.setLong(i++, data.getPublishTime());
            call.setLong(i++, data.getAppointUserId());
            String reward = PropertyHelper.parsePropertyToString(data.getReward());
            if(null == reward){
                reward = "";
            }
            call.setString(i++, reward);
            call.setInt(i++, data.getMotherBeautyId());
            call.setInt(i++, data.getChildOccupationId());
            call.setString(i++, data.getChildEarnSpeed().toString());
            call.setInt(i++, data.getChildOccupationAddition());
            call.setBoolean(i++, data.isTwins());
            call.setString(i++, data.getLimitEarnSpeed().toString());
            call.setLong(i++, data.getServerId());
            call.setLong(i++, data.getPartnerServerId());
            call.setString(i++, data.getMarriageConsume());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getChildUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected MarriageInfo parseData(ResultSet rs) throws SQLException {
        MarriageInfo data = new MarriageInfo();
        data.setUserId(rs.getLong("userId"));
        data.setUserNickName(rs.getString("userNickName"));
        data.setStatus(eMarriageStatusType.parse(rs.getInt("status")));
        data.setChildUid(rs.getString("childUid"));
        data.setChildTalent(rs.getInt("childTalent"));
        data.setChildName(rs.getString("childName"));
        data.setChildSex(rs.getInt("childSex"));
        data.setChildOccupation(rs.getInt("childOccupation"));
        data.setChildHeadIcon(rs.getInt("childHeadIcon"));
        data.setChildAdultTime(rs.getLong("childAdultTime"));
        data.setPublishTime(rs.getLong("publishTime"));
        data.setAppointUserId(rs.getLong("appointUserId"));
        data.setReward(PropertyHelper.parseStringToProperty(rs.getString("reward")));
        data.setMotherBeautyId(rs.getInt("motherBeautyId"));
        data.setChildOccupationId(rs.getInt("childOccupationId"));
        String value = rs.getString("childEarnSpeed");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setChildEarnSpeed(new BigInteger(value));
        }
        data.setChildOccupationAddition(rs.getInt("childOccupationAddition"));
        data.setTwins(rs.getBoolean("isTwins"));
        value = rs.getString("limitEarnSpeed");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setLimitEarnSpeed(new BigInteger(value));
        }
        data.setServerId(rs.getLong("serverId"));
        data.setPartnerServerId(rs.getLong("partnerServerId"));
        data.setMarriageConsume(rs.getString("marriageConsume"));
        return data;
    }
}
